package data.net;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import util.DateUtil;
import dataconnect.ConnectionFactory;
import dataconnect.SqlUtil;

public class StockStarReserch {

	public List<String> codeSetF = new ArrayList<String>();
	public Set<String> codeSet = new HashSet<String>();
	/**
	 * @param args
	 */
	public void handle(String code){
		System.out.println(code);
		
		String url = "http://stock.stockstar.com/report/data_all.aspx?stockcode="+code.substring(2, code.length());
		
		Document doc = null;
		try {
			doc = Jsoup.connect(url).get();
		} catch (IOException e) {
			System.out.println("超时："+code);
//			e.printStackTrace();
			codeSetF.add(code);
		}
		
		if(doc==null){
			return;
		}
		
		Elements els = doc.getElementsByClass("Extra_TBox");
		
		for (Element element : els) {
			Elements tabs = element.getElementsByTag("table");
			for (Element element2 : tabs) {
				Elements trs = element2.getElementsByTag("tr");
				for (int i = 2; i < trs.size(); i++) {
					Element tr = trs.get(i);
					Elements tds = tr.getElementsByTag("td");
					Date date = DateUtil.parseDate(tds.get(11).text());
					if(date.before(DateUtil.parseDate("2014-04-01"))){
						codeSetF.remove(code);
						return;
					}
					String aimPriStr = tds.get(5).text();
					if("-".equals(aimPriStr)){
						aimPriStr = "0";
					}
					String qsql = " select * from research where number='"+code+"' and date='"+tds.get(11).text()
							+"' and researcher='"+tds.get(3).text()+"'";
					Connection con = ConnectionFactory.getConnection();
					Statement stmt = null;
					ResultSet rs = null;
					try {
						stmt = con.createStatement();
						rs = stmt.executeQuery(qsql);
						if(rs.next()){
							codeSetF.remove(code);
							return;
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}try {
						if(rs!=null){
							rs.close();
						}
						if(stmt!=null){
							stmt.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
					String sql = "insert research values('"+UUID.randomUUID()+"','"+code+"','"+tds.get(2).text()+"','"+tds.get(3).text()+"','"+tds.get(4).text()
							+"',"+aimPriStr+","+tds.get(6).text()+","+tds.get(7).text().substring(0,tds.get(7).text().length()-1)+",'"+tds.get(11).text()+"','"+trs.get(i+1).text()+"') ";
					i=i+1;
					SqlUtil.update(sql);
					
				}
			}
		}
		codeSetF.remove(code);
	}
	
	public static void main(String[] args) {
		StockStarReserch ssr = new StockStarReserch();
		String sql = "select number from stockinfo where number not in ('sh000001','sz399001') ";
		Connection con = ConnectionFactory.getConnection();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
//				ssr.handle(rs.getString(1));
				ssr.codeSet.add(rs.getString(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		for (String code : ssr.codeSet) {
			ssr.handle(code);
		}
		
		while(!ssr.codeSetF.isEmpty()){
			ssr.handle(ssr.codeSetF.get(0));
		}
	}

}
